package com.auxgroup.hr.front.service.hireperson.impl;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.dao.domain.PsCHireEducatT;
import com.auxgroup.hr.front.dao.domain.PsCHirePrimaryClass;
import com.auxgroup.hr.front.dao.repository.PsCHireEducatTRepository;
import com.auxgroup.hr.front.service.hireperson.HireEducatService;

/** 
 * @author lvliuhao
 * @version 创建时间：180523 
 * 入职人员教育经历service
 */
@Service("hireEducatService")
public class HireEducatServiceImpl implements HireEducatService {
	private static final Logger logger = LoggerFactory.getLogger(HireEducatServiceImpl.class);
	
	@Autowired
	private PsCHireEducatTRepository PsCHireEducatTRepository;


	@Override
	public PsCHireEducatT getHireEducat(PsCHirePrimaryClass hirePrimary) {
		return PsCHireEducatTRepository.findOne(hirePrimary);
	}


	@Override
	public PsCHireEducatT save(PsCHireEducatT HireEducat) {
		if(HireEducat.getcSeqno()==null){
			HireEducat.setHrsRowAddOprid(HireEducat.getRsHireId());
			HireEducat.setHrsRowAddDttm(new Timestamp(System.currentTimeMillis()));
		}
		HireEducat.setHrsRowUpdOprid(HireEducat.getRsHireId());
		HireEducat.setHrsRowUpdDttm(new Timestamp(System.currentTimeMillis()));
		Set<String> ignoreFields = new HashSet<String>(Arrays.asList("cSeqno"));
		ObjectFieldUtils.handleNullValue(HireEducat, ignoreFields);
		return PsCHireEducatTRepository.save(HireEducat);
	}


	@Override
	public Boolean delete(PsCHirePrimaryClass hirePrimary) {
		try {
			PsCHireEducatTRepository.delete(hirePrimary);
		} catch (Exception e) {
			logger.error("delete HireEducat error: hirePrimary={}",hirePrimary,e);
			return false;
		}
		return true;
	}
	
	
	@Override
	public List<PsCHireEducatT> getAll(String rsHireId) {
		PsCHireEducatT HireEducat = new PsCHireEducatT();
		HireEducat.setRsHireId(rsHireId);
		return PsCHireEducatTRepository.findAll(Example.of(HireEducat),new Sort(Sort.Direction.DESC,"startDt"));
	}


	
	
	
}
